package algorithm;

/**
 * User: Asuka
 * Date: 11-3-1
 * Time: 下午10:15
 */
public class Perm {

    public static void main(String[] args) {
        perm(new Integer[]{1, 2, 3}, 0, 2);
    }

    public static <T> void perm(T[] array, int k, int m) {
        if (k == m) {
            for (int i = 0; i <= m; i++) {
                System.out.print(array[i]);
            }
            System.out.println();
        } else {
            for (int i = k; i <= m; i++) {
                swap(array, i, k);
                perm(array, k + 1, m);
                swap(array, i, k);
            }
        }
    }

    public static <T> void swap(T[] array, int i, int j) {
        T temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

}
